import router from '@/router'
import store from './store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { asyncRoutes } from '@/router/index'
// 导入封装好的项目配置文件
import getPageTitle from '@/utils/get-page-title'
router.beforeEach(async(to, from, next) => {
// 开启进度条
  NProgress.start()
  document.title = getPageTitle(to.meta.title)
  console.log(to, 30)
  const token = store.state.user.token
  const whiteList = ['/login', '/404']
  // 判断是否有token'
  if (token) {
    // 有token判断是否是去登录页面
    if (to.path === '/login') {
      console.log('已经登录，去首页')
      // 已登录去首页

      next('/')
      // 关闭进度条
      NProgress.done()
    } else {
      // 有id就不获取数据，没有就获取数据
      if (!store.state.user.userInfo.userId) {
        // 获取用户权限信息数据
        const menus = await store.dispatch('user/getUserInfo')
        console.log(menus, 'menus')
        console.log(asyncRoutes, 'asyncRoutes')
        // 使用权限信息筛选出用户可以访问的动态路由
        const filterRoutes = asyncRoutes.filter(route => {
          return menus.includes(route.children[0].name)
        })
        console.log(filterRoutes, 'filterRoutes')
        // 解决刷新404问题 将404添加到动态路由里面
        filterRoutes.push({ path: '*', redirect: '/404', hidden: true })
        // 需要权限的信息，根据权限信息动态的添加路由，渲染左侧菜单
        // 动态的添加用户可以访问的路由
        router.addRoutes(filterRoutes)
        // 将获取到的用户可以访问的动态路由存储到vuex里
        store.commit('menus/setMenus', filterRoutes)
        next({ ...to, replace: true })
      } else {
        // console.log(store, 10)
      // 去其他页面放行
        next()
      }
    }
  } else { // 没有token
    // 判断是否是去白名单
    if (whiteList.includes(to.path)) {
      // 去白名单放行
      next()
    } else {
      // 不去白名单，强行跳转到登录页面
      NProgress.done()
      next('/login')
    }
  }
})

router.afterEach(() => {
  // 关闭进度条
  NProgress.done()
})
// import router from './router'
// import store from './store'
// import { Message } from 'element-ui'
// import NProgress from 'nprogress' // progress bar
// import 'nprogress/nprogress.css' // progress bar style
// import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

// const whiteList = ['/login'] // no redirect whitelist

// router.beforeEach(async(to, from, next) => {
//   // start progress bar
//   NProgress.start()

//   // set page title
//   document.title = getPageTitle(to.meta.title)

//   // determine whether the user has logged in
//   const hasToken = getToken()

//   if (hasToken) {
//     if (to.path === '/login') {
//       // if is logged in, redirect to the home page
//       next({ path: '/' })
//       NProgress.done()
//     } else {
//       const hasGetUserInfo = store.getters.name
//       if (hasGetUserInfo) {
//         next()
//       } else {
//         try {
//           // get user info
//           await store.dispatch('user/getInfo')

//           next()
//         } catch (error) {
//           // remove token and go to login page to re-login
//           await store.dispatch('user/resetToken')
//           Message.error(error || 'Has Error')
//           next(`/login?redirect=${to.path}`)
//           NProgress.done()
//         }
//       }
//     }
//   } else {
//     /* has no token*/

//     if (whiteList.indexOf(to.path) !== -1) {
//       // in the free login whitelist, go directly
//       next()
//     } else {
//       // other pages that do not have permission to access are redirected to the login page.
//       next(`/login?redirect=${to.path}`)
//       NProgress.done()
//     }
//   }
// })

// router.afterEach(() => {
//   // finish progress bar
//   NProgress.done()
// })
